package com.personal.system.service.impl.content.web;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.personal.common.result.PageUtils;
import com.personal.common.result.Result;
import com.personal.system.config.SecurityUtil;
import com.personal.system.constant.MessageConstant;
import com.personal.system.entity.content.ImMessage;
import com.personal.system.entity.vo.ImMessageVO;
import com.personal.system.mapper.content.ImMessageMapper;
import com.personal.system.service.content.web.ApiImMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class ApiImMessageServiceImpl implements ApiImMessageService {

    private final ImMessageMapper imMessageMapper;
    
    private final SecurityUtil securityUtil;

    @Override
    public Result getNewSystemNotice() {
        String userId = securityUtil.getUserIdWeb();
        long systemCount=0,commentCount=0,watchCount=0,likeCount=0,collectCount=0,privateCount=0;
        if (StrUtil.isNotBlank(userId)){
            systemCount = imMessageMapper.selectCount(new LambdaQueryWrapper<ImMessage>()
                    .eq(ImMessage::getCode, MessageConstant.SYSTEM_MESSAGE_CODE).eq(ImMessage::getToUserId, userId)
                    .eq(ImMessage::getIsRead, 0).eq(ImMessage::getNoticeType, MessageConstant.MESSAGE_SYSTEM_NOTICE));

            commentCount = imMessageMapper.selectCount(new LambdaQueryWrapper<ImMessage>()
                    .eq(ImMessage::getCode, MessageConstant.SYSTEM_MESSAGE_CODE).eq(ImMessage::getToUserId, userId)
                    .eq(ImMessage::getIsRead, 0).eq(ImMessage::getNoticeType, MessageConstant.MESSAGE_COMMENT_NOTICE));

            watchCount = imMessageMapper.selectCount(new LambdaQueryWrapper<ImMessage>()
                    .eq(ImMessage::getCode, MessageConstant.SYSTEM_MESSAGE_CODE).eq(ImMessage::getToUserId, userId)
                    .eq(ImMessage::getIsRead, 0).eq(ImMessage::getNoticeType, MessageConstant.MESSAGE_WATCH_NOTICE));

            likeCount = imMessageMapper.selectCount(new LambdaQueryWrapper<ImMessage>()
                    .eq(ImMessage::getCode, MessageConstant.SYSTEM_MESSAGE_CODE).eq(ImMessage::getToUserId, userId)
                    .eq(ImMessage::getIsRead, 0).eq(ImMessage::getNoticeType, MessageConstant.MESSAGE_LIKE_NOTICE));

            collectCount = imMessageMapper.selectCount(new LambdaQueryWrapper<ImMessage>()
                    .eq(ImMessage::getCode, MessageConstant.SYSTEM_MESSAGE_CODE).eq(ImMessage::getToUserId, userId)
                    .eq(ImMessage::getIsRead, 0).eq(ImMessage::getNoticeType, MessageConstant.MESSAGE_COLLECT_NOTICE));

            privateCount = imMessageMapper.selectCount(new LambdaQueryWrapper<ImMessage>()
                    .eq(ImMessage::getCode, MessageConstant.PRIVATE_CHAT_CODE).eq(ImMessage::getToUserId, userId)
                    .eq(ImMessage::getIsRead, 0));
        }


        Map<String, Long> map = new HashMap<>();
        map.put("system", systemCount);
        map.put("comment", commentCount);
        map.put("watch", watchCount);
        map.put("like", likeCount);
        map.put("collect", collectCount);
        map.put("private", privateCount);

        return Result.ok(map);
    }

    @Override
    public Result getMessageNotice(Integer type) {
        String userId = securityUtil.getUserIdWeb();
        Page<ImMessageVO> page = imMessageMapper.getMessageNotice(new Page<>(PageUtils.getPageNo(), PageUtils.getPageSize()),
                userId, type);
        page.getRecords().forEach(item -> {
            item.setCreateTimeStr(DateUtil.format(item.getCreateTime(),"yyyy-MM-dd HH:mm"));
        });
        // 修改该类型的所有消息为已读
        ImMessage message = ImMessage.builder().isRead(1).build();
        imMessageMapper.update(message, new LambdaQueryWrapper<ImMessage>().eq(ImMessage::getToUserId, userId)
                .eq(ImMessage::getNoticeType, type));
        return Result.ok(page);
    }

    @Override
    public Result deleteByNoticeType(String id, Integer type) {
        if (StringUtils.isNotBlank(id)) {
            imMessageMapper.deleteById(id);
            return Result.ok();
        }
        imMessageMapper.delete(new LambdaQueryWrapper<ImMessage>().eq(ImMessage::getToUserId, securityUtil.getUserIdWeb())
                .eq(ImMessage::getNoticeType, type));
        return Result.ok();
    }

}
